/*

 AdWhirlAdapterTapad.m
 
 Copyright 2011 Tapad, Inc.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 
*/

#import "AdWhirlView.h"
#import "AdWhirlConfig.h"
#import "AdWhirlAdNetworkConfig.h"
#import "AdWhirlDelegateProtocol.h"
#import "AdWhirlLog.h"
#import "AdWhirlAdNetworkAdapter+Helpers.h"
#import "AdWhirlAdNetworkRegistry.h"
#import "AdWhirlAdapterCustom.h"
#import "AdWhirlError.h"
#import "CJSONDeserializer.h"
#import "AdWhirlCustomAdView.h"


#import "TapadAdRequest.h"
#import "TapadAdSDK.h"
#import "AdWhirlAdapterTapad.h"

@implementation AdWhirlAdapterTapad

@synthesize bannerView;

// required by AdWhirl at initialization time. AdWhirl manages a container with
// all the ad networks supported by the publisher's app, and configured  
// on the publisher's account on the AdWhirl website. 
// To create the correct implementation instance for the network at runtime,
// it needs to invoke this method during configuration time, so it knows how
// to send alloc/init messages to the right object.  
+ (void)load {
    [[AdWhirlAdNetworkRegistry sharedRegistry] registerClass:self];
}


// required by AdWhirl
+ (AdWhirlAdNetworkType)networkType {
    return AdWhirlAdNetworkTypeTapAd; // this value is defined in that famous enum
}

// convenience method for extracting tapad specific ids
- (NSObject *)delegateValueForSelector:(SEL)selector {
    return ([adWhirlDelegate respondsToSelector:selector]) ?
    [adWhirlDelegate performSelector:selector] : nil;
}


// required by AdWhirl
- (void)getAd {
    TapadAdRequest* adRequest = [TapadAdSDK newAdRequest];
    NSObject* value;
    
    if ((value = [self delegateValueForSelector:@selector(tapadPublisherId)])) {
        adRequest.publisherId = (NSString*)value;
    }
    if ((value = [self delegateValueForSelector:@selector(tapadPropertyId)])) {
        adRequest.propertyId = (NSString*)value;
    }
    if ((value = [self delegateValueForSelector:@selector(tapadPlacementId)])) {
        adRequest.placementId = (NSString*)value;
    }
    adRequest.wrapHtml = true; // get rid of the margin

    // configure the banner view
    self.bannerView = [[TapadBannerView alloc] initWithFrame:adWhirlView.bounds];
    self.bannerView.delegate = self;
    self.bannerView.rootViewController = [adWhirlDelegate viewControllerForPresentingModalView];

    // load the ad!!!
    [bannerView loadRequest:adRequest];
    
    [adRequest release];
}

// adwhirl base class override
- (void) stopBeingDelegate {
    NSLog(@"%@:stopBeingDelegate",[self class]);
    if (self.bannerView) {
        self.bannerView.delegate = nil;
        self.bannerView = nil;
    }
}


#pragma mark <TapadBannerViewDelegate> protocol

// sent when the ad has been loaded from the server, and it is about to be
// sent to the view for display. at this point, the delegate may want to perform
// various view movements and integrations, like in this case with adwhirl.
// assigning the controller's view to the adwhirl property, makes the view appear, 
// as it is added to that view hierarchy
-(void)adWillAppear:(TapadBannerView*)view {
    NSLog(@"%@:adWillAppear",[self class]);
    self.adNetworkView = view;
}

- (void)didReceiveAd:(TapadBannerView *)view {
    NSLog(@"%@:didReceiveAd",[self class]);
    [self.adWhirlView adapter:self didReceiveAdView:view];
}

- (void)didFailToReceiveAd:(TapadBannerView *)view withError:(NSError*)error {
    NSLog(@"%@:didFailToReceiveAd",[self class]);
    [self.adWhirlView adapter:self didFailAd:error];
}

- (void)willPresentScreen:(TapadBannerView *)adView {
    NSLog(@"%@:willPresentScreen",[self class]);
    [self helperNotifyDelegateOfFullScreenModal];
}

// Sent just after dismissing a full screen view.  Use this opportunity to
// restart anything you may have stopped as part of adViewWillPresentScreen:.
- (void)didDismissScreen:(TapadBannerView *)adView {
    NSLog(@"%@:didDismissScreen",[self class]);
    [self helperNotifyDelegateOfFullScreenModalDismissal];
}

-(void) willDismissScreen:(TapadBannerView *)adView {}

-(void) willLeaveApplication:(TapadBannerView *)adView {}

@end
